import qrcode from 'yaqrcode'

export default function getData() {
    let productionInfoEl = document.querySelectorAll('#productDetails_detailBullets_sections1 > tbody > tr > td')
    let filterAsinFromProductInfo = Array.from(productionInfoEl).map(item => item.textContent.trim()).filter(item => {
        return item.match(/B0[\w\d]{8}/)
    })

    if(productionInfoEl.length === 0) {
        productionInfoEl = document.querySelectorAll('#detailBullets_feature_div > ul > li > span > span:nth-child(2)')
        filterAsinFromProductInfo = Array.from(productionInfoEl).map(item => item.textContent.trim()).filter(item => {
            return item.match(/B0[\w\d]{8}/)
        })
    }

    const asin = filterAsinFromProductInfo[0]

    const titleEl = document.querySelector('#productTitle')
    let title 
    if(titleEl) {
        title = titleEl.textContent.trim()
    }

    const imageEl = document.querySelector('#landingImage')
    let image
    if(imageEl) {
        image = imageEl.src
    }

    // #corePrice_desktop > div > table > tbody > tr > td.a-span12 > span.a-price.a-text-price.a-size-medium.apexPriceToPay > span.a-offscreen
    // const priceEl = document.querySelector('#corePriceDisplay_desktop_feature_div > div.a-section.a-spacing-none.aok-align-center > span > span.a-offscreen')
    // #corePriceDisplay_desktop_feature_div > div.a-section.a-spacing-none.aok-align-center > span.a-price.aok-align-center.reinventPricePriceToPayPadding.priceToPay > span.a-offscreen
    // #corePriceDisplay_desktop_feature_div > div.a-section.a-spacing-none.aok-align-center > span.a-price.aok-align-center.reinventPricePriceToPayMargin.priceToPay > span.a-offscreen
    let priceEl = document.querySelector('#corePrice_desktop > div > table > tbody > tr > td.a-span12 > span.a-price.a-text-price.a-size-medium.apexPriceToPay > span.a-offscreen')
    if(!priceEl) {
        priceEl = document.querySelector('#corePriceDisplay_desktop_feature_div > div.a-section.a-spacing-none.aok-align-center > span.a-price.aok-align-center.priceToPay > span.a-offscreen')
    }
    let price = -1
    if(priceEl) {
        price = parseFloat(priceEl.textContent.match(/[\d|.]+/)[0])
    }

    const shopEl = document.querySelector('#bylineInfo')
    // const firstMatch = shopEl.href.match(/\/stores\/(.*)\/page\//)
    // console.log('firstMatch', firstMatch, shopEl)
    // let shopName
    // if(firstMatch) {
    //     shopName = firstMatch[1]
    // } else {
    //     const secondMatch = shopEl.href.match(/.com\/([^/]*)\//)
    //     shopName = secondMatch[1]
    // }
    const shopNameEl = document.querySelector('#sellerProfileTriggerId')
    let shopName, shopLink
    if(shopNameEl) {
        shopName = shopNameEl.textContent
    }
    if(shopEl) {
        if(shopEl.textContent.indexOf('Brand') === 0) {
            shopName = shopEl.textContent.match(/^Brand: (.*)/)[1]
        } else {
            shopName = shopEl.textContent.match(/^Visit the (.*) Store$/)[1]
        }
        shopLink = shopEl.href
    }
    // if(shopEl) {
    // }
    const shop = {
        name: shopName,
        link: shopLink
    }

    // #productDetails_detailBullets_sections1 > tbody > tr:nth-child(6) > td > span > span:nth-child(1)
    let topRankEl = document.querySelector('#detailBulletsWrapper_feature_div > ul:nth-child(4) > li > span')
    let topRankMatch, topRank = {}
    if(topRankEl !== null && topRankEl.childNodes && topRankEl.childNodes.length >= 2) {
        topRankMatch = topRankEl.childNodes[2].textContent.match(/#([\d|,]+) in (.*) \(/)
        topRank = {
            rank: parseInt(topRankMatch[1].replace(',', '')),
            cat: topRankMatch[2]
        }
    }

    if(topRankEl === null) {
        topRankEl = document.querySelector('#productDetails_detailBullets_sections1 > tbody > tr > td > span > span:nth-child(1)')
        if(topRankEl && topRankEl.childNodes && topRankEl.childNodes.length >= 1) {
            topRankMatch = topRankEl.childNodes[0].textContent.match(/#([\d|,]+) in (.*) \(/)
            topRank = {
                rank: parseInt(topRankMatch[1].replace(',', '')),
                cat: topRankMatch[2]
            }
        }
    }

    let subRankEl = document.querySelector('#detailBulletsWrapper_feature_div > ul:nth-child(4) > li > span > ul > li > span')
    let subRankStr, subRankMatch, subRank = {}
    if(subRankEl) {
        subRankStr = subRankEl.textContent
        subRankMatch = subRankStr.match(/#([\d|,]+) in (.*)/)
        subRank = {
            rank: parseInt(subRankMatch[1].replace(',', '')),
            cat: subRankMatch[2]
        }
    }

    if(subRankEl === null) {
        subRankEl = document.querySelector('#productDetails_detailBullets_sections1 > tbody > tr > td > span > span:nth-child(3)')
        if(subRankEl) {
            subRankStr = subRankEl.textContent
            subRankMatch = subRankStr.match(/#([\d|,]+) in (.*)/)
            subRank = {
                rank: parseInt(subRankMatch[1].replace(',', '')),
                cat: subRankMatch[2]
            }
        }
        // #productDetails_detailBullets_sections1 > tbody > tr:nth-child(6) > td > span > span:nth-child(3)
    }

    const sellerProfileEl = document.querySelector('#sellerProfileTriggerId')
    let sellerId
    if(sellerProfileEl) {
        const sellerMatched = sellerProfileEl.href.match(/seller=(\w+)/)
        if(sellerMatched) {
            sellerId = sellerMatched[1]
        }
    }

    const videoEl = document.querySelectorAll('div[data-video-url]')
    const videoList = Array.from(videoEl).map(item => {
        const html = item.innerHTML
        // console.log('item', item)
        const itemDataset = item.dataset
        const imageEl = document.createElement('div')
        imageEl.innerHTML = html
        const imageEl2 = item.querySelector('a > div.vse-video-image')
        let imageUrl
        if(imageEl2) {
            console.log('imageEl2', imageEl2.dataset)
            imageUrl = imageEl2.dataset.img
        } else {
            imageUrl = item.dataset.thumbnailUrl
        }
        const videoUrl = itemDataset.videoUrl.replace('.hls.m3u8', '.mp4.480.mp4')
        return {
            videoUrl,
            videoUrlBase64: qrcode(videoUrl),
            html,
            imageUrl
        }
    })

    return {
        asin,
        title,
        subRank,
        topRank,
        image,
        price,
        shop,
        sellerId,
        videoList
    }
}
